﻿using System.Net;
using System.Web;
using Shinkansen.Model;

namespace Shinkansen.Runtime
{
    public class OutputProcessor
    {
        private readonly HttpContext _context;
        private readonly HeaderSetup _headers;

        /// <summary>
        /// Initializes a new instance of the <see cref="OutputProcessor"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="resourceInfo">The resource info.</param>
        public OutputProcessor (HttpContext context, ResourceInformation resourceInfo)
        {
            _context = context;
            _headers = new HeaderSetup (context, resourceInfo);
        }

        /// <summary>
        /// Sets the headers.
        /// </summary>
        public void SetHeaders ()
        {
            _headers.SetContentEncodingHeader ();
            _headers.SetCachingHeaders ();
            _headers.SetContentTypeHeader ();
        }

        /// <summary>
        /// Sends the content.
        /// </summary>
        /// <param name="finalOutput">The final output.</param>
        public void SendContent (byte[] finalOutput)
        {
            if (finalOutput.Length == 0)
            {
                _context.Response.StatusCode = (int)HttpStatusCode.NoContent;
            }
            else
            {
                _context.Response.BinaryWrite (finalOutput);
            }

            _context.Response.End ();
        }
    }
}